package com.evl.redis;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;

import com.evl.dictionaryConfig.DictConfig;
import com.evl.dictionaryConfig.DictConst;

@Configuration
@EnableCaching
public class RedisConfig {

    /**
     * 连接Redis
     * 
     * @return
     */
    public static JedisConnectionFactory redisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        // host地址
        jedisConnectionFactory.setHostName(DictConfig.getKey(DictConst.redis_hostname));
        jedisConnectionFactory.setPassword(DictConfig.getKey(DictConst.redis_password));
        // 端口号
        jedisConnectionFactory.setPort(Integer.parseInt(DictConfig.getKey(DictConst.redis_port)));
        
        jedisConnectionFactory.setTimeout(Integer.parseInt(DictConfig.getKey(DictConst.redis_timeout)));
        
        jedisConnectionFactory.afterPropertiesSet();
        return jedisConnectionFactory;
    }

    /**
     * RedisTemplate配置
     * 
     * @param redisCF
     * @return
     */
    public static RedisTemplate<String, Object> redisTemplate(
            RedisConnectionFactory redisCF) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        redisTemplate.setConnectionFactory(redisCF);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}
